package com.example.administrator.myapplication.View.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.Bean.Notice;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Presenter.NoticePresenter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;
import com.example.administrator.myapplication.View.Fragment.INoticeView;
import com.example.administrator.myapplication.View.layout.MineItemView;
import com.example.administrator.myapplication.View.layout.NoticeItemView;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2019/10/8 0008.
 */

public class NoticeActivity extends AppCompatActivity implements INoticeView,MineItemView.OnRootClickListener,View.OnClickListener{
    private LinearLayout ll_root;
    private TextView tv_back;
    private TextView toolbar_title;
    private Notice notice;
    private NoticePresenter noticePresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#d16d26"));
        }
        initView();

    }

    private void initView() {
        tv_back = findViewById(R.id.toolbar_back);
        ll_root = findViewById(R.id.ll_root);
        toolbar_title = findViewById(R.id.toolbar_title);
        toolbar_title.setText(getIntent().getStringExtra("title"));
        notice = new Notice();
        notice.setNoticeType(getIntent().getStringExtra("type"));
        noticePresenter = new NoticePresenter(this);
        noticePresenter.getNotices(notice);
        tv_back.setOnClickListener(this);

    }

    @Override
    public void onRootClick(View view) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.toolbar_back:
                finish();
                break;
        }
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showNotices(List<Notice> notices) {
        for(int i = 0;i<notices.size();i++){
            final Notice n = notices.get(i);
            ll_root.addView(new NoticeItemView(this)
                    .init(R.drawable.splash,n.getNoticeTitle(),n.getNoticeContent(),new SimpleDateFormat("MM-dd").format(n.getUpdateTime()),false)
                    .setRootOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getApplicationContext(),NoticeDetailActivity.class);
                            intent.putExtra("notice",n);
                            startActivity(intent);
                        }
                    })
            );
        }
    }
}
